<?php
namespace Util;
/**
 * [WechatRelay description]
 * @Author   :seven
 * @DateTime :2016-09-18T10:29:42+0800
 * @Detail   :向公众号消息转发,及底部栏菜单开发
 */
    class  WechatRelay{
        /*用户在公众号设置的TOKEN*/
        private $TOKEN = 'seven';
        private $accessToken = '';

        public function __construct($APPID = NULL, $APPSECRET = NULL){
            $wt = new WechatTicket();
            $wt->set($APPID,$APPSECRET);
            $this->accessToken = $wt->getAccessToken();
        }

        /**
         * [Relay 获取信息并转发]
         * @Author   :seven
         * @DateTime :2016-09-18T15:03:05+0800
         * @Detail   :获取公众号信息并转发
         */
        public function Relay($openId){
            $msg = $this->getMsg();
            return $this->sendMsg($openId,$msg['type'],$msg['content']);
        }

        /**
         * [getMsg 获取对公众号发送的消息]
         * @Author   :seven
         * @DateTime :2016-09-18T10:31:23+0800
         * @Detail   :
         * @return   [type]                    [description]
         */
        public function getMsg(){
            $echoStr = $_GET["echostr"];
            
            if($this->checkSignature()){
                $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
                libxml_disable_entity_loader(true);
                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                
                $Msg = array();
                /*发送者OPENID*/
                $Msg['from'] = trim($postObj->FromUserName);
                /*消息类型*/
                $Msg['type'] = trim($postObj->MsgType);
                /*开发者OPENID*/
                $Msg['to'] = trim($postObj->ToUserName);
                if($Msg['type'] == 'voice' || $Msg['type'] == 'image'){
                    $Msg['content'] = trim($postObj->MediaId);
                }else{
                    $Msg['content'] =  trim($postObj->Content);
                }
            }else{
                $Msg['errCode'] = '10010';
                $Msg['errMsg'] = '微信TOKEN设置错误';
            }
            echo $echoStr;
            return $Msg;
        }
        /**
         * [sendMsg 公众号转发信息]
         * @Author   :seven
         * @DateTime :2016-09-18T10:49:57+0800
         * @Detail   :
         * @param    [type]         $to      [目标用户]
         * @param    [type]         $type    [消息类型]
         * @param    [type]         $content [消息内容]
         * @return   [type]                  [发送结果]
         */
        public function sendMsg($to,$type,$content){
            $msg['touser'] = $to;
            if($type == 'text'){
                $msg['msgtype'] = 'text';
                $msg['text']['content'] = urlencode($content);
            }else{
                $msg['msgtype'] = $type.'';
                $msg[$type.'']['media_id'] = urlencode($content);
            }

            $hc = new \Util\Httpclient();
            $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$this->accessToken}";
            return $hc->quickPost($url
                ,urldecode(json_encode($msg)));
        }


        public function CreateButton($btnMsg){
            $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$this->accessToken}";
            $hc = new \Util\Httpclient();
            return $hc->quickPost($url
                ,urldecode(json_encode($btnMsg)));
        }

        private function checkSignature(){
            $signature = $_GET["signature"];
            $timestamp = $_GET["timestamp"];
            $nonce = $_GET["nonce"];
                    
            $tmpArr = array($this->TOKEN, $timestamp, $nonce);

            sort($tmpArr, SORT_STRING);
            $tmpStr = implode( $tmpArr );
            $tmpStr = sha1( $tmpStr );
            
            if( $tmpStr == $signature ){
                return true;
            }else{
                return false;
            }
        }
    }
